$(function () {
    // 页面加载完成后获取最近订单数据
    loadRecentOrders();
});

// 加载最近订单数据
function loadRecentOrders() {
    $.ajax({
        url: "getRecentOrders",
        type: "Post",
        dataType: "json",
        success: function (data) {
            showRecentOrders(data);
        },
        error: function () {
            $("#recentOrders").html("<div class='empty-tip'>获取订单数据失败</div>");
        }
    });
}

// 显示最近订单
function showRecentOrders(orders) {
    let html = "";

    if (!orders || orders.length == 0) {
        html += "<div class='empty-tip'>暂无最近订单</div>";
    } else {
        html += "<div class='orders-list'>";
        $.each(orders, function (index, order) {
            if (order.hotel) {
                html += getHotelOrderHtml(order.hotel);
            } else if (order.scenicSpot) {
                html += getScenicOrderHtml(order.scenicSpot);
            } else if (order.tickets) {
                html += getTicketOrderHtml(order.tickets);
            }
        });
    }
    $("#recentOrders").html(html);
}

// 生成酒店订单HTML
function getHotelOrderHtml(hotel) {
    const html =
        '<div class="order-row">' +
        '<div class="order-type">酒店</div>' +
        '<div class="order-main">' +
        '<div class="hotel-info">' +
        '<div>酒店名称: ' + hotel.brand + '</div>' +
        '<div>房间数: ' + hotel.roomNum + '</div>' +
        '<div>预订人: ' + hotel.contactName + '</div>' +
        '<div>联系电话: ' + hotel.contactPhone + '</div>' +
        '<div>状态: <span class="status-' + hotel.status + '">' + hotel.status + '</span></div>' +
        '</div>' +
        '</div>' +
        '<div class="order-price">￥' + hotel.price + '</div>' +
        '</div>';
    return html;
}

// 生成景点订单HTML
function getScenicOrderHtml(scenic) {
    const html =
        '<div class="order-row">' +
        '<div class="order-type">景点</div>' +
        '<div class="order-main">' +
        '<div class="scenic-name">' + scenic.scenicName + '</div>' +
        '<div class="scenic-address">地址: ' + scenic.scenicAddress + '</div>' +
        '</div>' +
        '<div class="order-price">￥' + scenic.price + '</div>' +
        '</div>';
    return html;
}

// 生成车票订单HTML
function getTicketOrderHtml(ticket) {
    const html =
        '<div class="order-row">' +
        '<div class="order-type">单程' + ticket.type + '</div>' +
        '<div class="order-main">' +
        '<div class="route">' + ticket.departure_city + ' → ' + ticket.arrival_city + '</div>' +
        '<div class="time">出发时间: ' + ticket.departure_time + '</div>' +
        '<div class="duration">行程时间: ' + ticket.start_time + ' - ' + ticket.end_time + '</div>' +
        '</div>' +
        '<div class="order-price">￥' + ticket.price + '</div>' +
        '</div>';
    return html;
}